<?php

namespace Parse;

class StringParser extends Parser
{
    private $sub_str;
    
    public function __construct($sub_str, $callback = null)
    {
        if (!is_string($sub_str)) {
            throw new ParseException("StringParser must operate on a string, not "
                    . var_export($sub_str, true));
        }
        $this->sub_str = $sub_str;
        parent::__construct($callback);
    }

    public function defaultCallback()
    {
        return func_get_arg(0);
    }

    public function matchProcedure($string)
    {
        $sub_str_pos = strpos($string, $this->sub_str);
        if ($sub_str_pos != 0) {
            throw new ParseException("could not match string");
        }
        if (!(substr($string, 0, strlen($this->sub_str))
              === $this->sub_str)) {
            throw new ParseException("could not match string");
        }
            return array(
            "matches" => array($this->sub_str),
            "rest" => substr($string, strlen($this->sub_str)));
    }
}
